<?php
/**
 * Helper class to control all of the exporting of references to various sources.
 *
 * @copyright &copy; 2009 The Open University
 * @author j.platts@open.ac.uk
 * @author Author_Name_2@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package references
 */
GLOBAL $CFG;
require_once(dirname(__FILE__).'/module_resourcepage.php');
require_once($CFG->dirroot.'/local/references/export.php');
require_once($CFG->dirroot.'/local/references/apibib/apibib_lib.php');

class resourcepage_export_references extends references_export{
    public static $inst;

    private $module;
    private $id;
    private $resourcepage;
    private $course;
    private $cm;

    //NULL if not submitted to this page from itself, or name of option if did
    private $submitted;

    public function __construct(){

        GLOBAL $COURSE, $CFG, $db;

        $this->initexport();

        $module=module_resourcepage::$inst;
        $this->module=&$module;

        if(!confirm_sesskey()){
            print_error('confirmsesskeybad');
        }

        $id=required_param('cmid',PARAM_INT);    // Course Module ID
        $this->id=$id;

        $module->obtain_view_info($id,null,$cm,$course,$resourcepage);

        $this->resourcepage=$resourcepage;
        $this->course=$course;
        $this->cm=$cm;

        require_login($cm->course,true,$cm);

        //check if coming to this page after submitting one of the export options
        $this->submitted=$this->return_submitted();
        if($this->submitted==NULL){
            $this->prnt_header();

            //check idlist is not empty (no options selected)
            $idlist=required_param('idlist',PARAM_SEQUENCE);
            if($idlist==''){
                //no option selected error
                print_error('refexport_error_noneselected','references','view.php?id='.$this->id);
            }

            $formopts=new stdClass;
            $formopts->hidden=array('cmid','idlist');
            $formopts->hiddenvals=array($id,$idlist);
            print $this->return_export_menu($formopts);
            $this->prnt_footer();
            
            add_to_log($course->id,'resourcepage','view','','Access reference export menu',$id);
        }else{
            //page has been submitted to itself do something
            $submittedtype=$this->return_format_export_type($this->submitted);
            //check idlist is not empty (no options selected)
            $idlist=required_param('idlist',PARAM_SEQUENCE);
            if($idlist==''){
                //no option selected error
                print_error('refexport_error_noneselected','references','view.php?id='.$this->id);
            }
            $idlist=rtrim($idlist,'\,');
            //join up all the selected options xml from db into xml
            $refids=explode(',',$idlist);

            //create references xml that we will send to export
            $data='<?xml version="1.0" encoding="utf-8" standalone="no"?><references>';

            $options=new stdClass();
            $options->titles=array();
            $options->links=array();
            $options->styles='';
            $options->filename='';
            $options->coursename='';
            $options->redirect='';

            //now go thru all the ids and get data from the resourcepage_items table in db
            for($a=0,$max=count($refids);$a<$max;$a++){
                //connect to db and get row from table
                if(!$rs=get_recordset_sql('select data,weblink,title from '.$CFG->prefix.'resourcepage_items where id='.$refids[$a],false)) {
                    throw new Exception('module_resourcepage SQL error: '.
                    $db->ErrorMsg());
                }
                //get data xml as string, turn to xml and import node into $data
                $data.=$rs->fields['data'];
                //get the weblink and add to array
                $options->links[]=$rs->fields['weblink'];
                //get the title and add to array
                $options->titles[]=$rs->fields['title'];
            }

            $data.='</references>';

            //get the style and filename (from resource page name)

            if(!$rs=get_recordset_sql('select refstyle,name from '.$CFG->prefix.'resourcepage where id='.$this->resourcepage->id,false)) {
                throw new Exception('module_resourcepage SQL error: '.
                $db->ErrorMsg());
            }

            if(!$teststyle=$rs->fields['refstyle']){
                $teststyle=0;
            }
            //convert refstyle from int into text
            $options->styles=apibib::$referencestyles[$teststyle]['string'];

            //get name of resource page
            $rname=$rs->fields['name'];

            $options->filename=$this->course->shortname.'_'.$rname;

            $options->coursename=$this->course->shortname;

            $options->redirect=$CFG->wwwroot.'/mod/resourcepage/view.php?id='.$this->id;

            //print header if export type as there may be user interaction
            if($submittedtype==self::EXPORT){
                $this->prnt_header();
            }

            add_to_log($course->id,'resourcepage','view','','Running export '.$this->submitted.' on reference ids '.$idlist,$id);
            
            //call export function
            if($this->start_export_action($this->submitted,$data,$options)===false){
                print_error('refexport_error','references','view.php?id='.$this->id);
            }

            //print footer as an export fucntion
            if($submittedtype==self::EXPORT){
                $this->prnt_footer();
            }

        }

    }

    /**
     * Prints a heading and start of a box
     * @return unknown_type
     */
    private function prnt_header(){
        $title=get_string('reference_exporttitle','references');
        $this->module->print_header($this->cm,$this->course,$this->resourcepage,$title,false,'','',$this->return_headerlibs());
        print_heading_with_help($title, "exportreference", 'resourcepage');

        print_box_start();
    }

    /**
     * Prints end of a box and footer
     * @return unknown_type
     */
    private function prnt_footer(){
        print_box_end();

        print_footer($this->course);
    }
}
resourcepage_export_references::$inst=new resourcepage_export_references();
?>